package com.gzhryc.net_marketing.cloud.account.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.account.dao.TransferMoneyOrderDao;
import com.gzhryc.net_marketing.cloud.account.dao.db.TransferMoneyOrder;
import com.gzhryc.net_marketing.cloud.account.dao.enums.TransferMoneyOrderEnum;
import org.apache.commons.lang3.time.DateUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class TransferMoneyOrderService {

    static Logger log = Logger.getLogger(TransferMoneyOrderService.class);

    public static TransferMoneyOrderService self() {
        return MultiDBTools.getService(MarketConstants.JDBC_KEY,TransferMoneyOrderService.class, true);
    }

    TransferMoneyOrderDao dao;

    public TransferMoneyOrderService(String jdbcKey) {
        this.dao = new TransferMoneyOrderDao(jdbcKey);
    }

    public List<TransferMoneyOrder> findProcessingBy2Hour(Integer pageNo,Integer pageLimit){
        Date endDate = DateUtils.addHours(new Date(),-2);
        Conditions conditions = new Conditions(TransferMoneyOrder.class);
        conditions.and().is("state",TransferMoneyOrderEnum.State.Processing.index());
        conditions.and().le("createDate",endDate);

        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(TransferMoneyOrder order){
        order.setNote("用户取款");
        order.setState(TransferMoneyOrderEnum.State.Wait.index());
        order.setCreateDate(new Date());

        try {
            return dao.insert(order) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean waitToProcessing(String tradeNo){
        if(StringTools.isNotBlank(tradeNo)){
            Conditions conditions = new Conditions(TransferMoneyOrder.class);
            conditions.and().is("tradeNo",tradeNo);
            conditions.and().is("state", TransferMoneyOrderEnum.State.Wait.index());

            TransferMoneyOrder updateEntity = new TransferMoneyOrder();
            updateEntity.setState(TransferMoneyOrderEnum.State.Processing.index());

            try {
                return dao.updateNotNull(updateEntity,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean processingToWait(String tradeNo){
        if(StringTools.isNotBlank(tradeNo)){
            Conditions conditions = new Conditions(TransferMoneyOrder.class);
            conditions.and().is("tradeNo",tradeNo);
            conditions.and().is("state", TransferMoneyOrderEnum.State.Processing.index());

            TransferMoneyOrder updateEntity = new TransferMoneyOrder();
            updateEntity.setState(TransferMoneyOrderEnum.State.Wait.index());

            try {
                return dao.updateNotNull(updateEntity,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean updateExternalInfo(String tradeNo,String externalTradeNo,String externalContent,String transferAccount){
        if(StringTools.isNotBlank(tradeNo)){
            Conditions conditions = new Conditions(TransferMoneyOrder.class);
            conditions.and().is("tradeNo",tradeNo);
            conditions.and().is("state", TransferMoneyOrderEnum.State.Processing.index());

            TransferMoneyOrder updateEntity = new TransferMoneyOrder();
            updateEntity.setExternalTradeNo(externalTradeNo);
            updateEntity.setExternalContent(externalContent);
            updateEntity.setTransferAccount(transferAccount);
            updateEntity.setTransferDate(new Date());
            try {
                return dao.updateNotNull(updateEntity,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
